package com.tgy.admin.Service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.tgy.admin.Service.AdminUserService;
import com.tgy.admin.util.BeanFieldUtils;
import com.tgy.core.service.BlogUserService;
import com.tgy.page.PageQuery;
import com.tgy.pojo.BlogUser;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 21:04
 *
 * 后台用户业务实现
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private BlogUserService blogUserService;
    @Resource
    private BeanFieldUtils beanFieldUtils;
    @Value("${tgy.userPassword}")
    private String userPassword;


    @Override
    public LayuiTableResult getBlogUserList(BlogUser bo, PageQuery pageQuery) {
        return blogUserService.queryLayuiPageList(bo, pageQuery);
    }


    @Override
    public GraceJSONResult addBlogUser(BlogUser bo) {
        try {
            beanFieldUtils.add(bo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return GraceJSONResult.ok(blogUserService.insertByBo(bo));
    }

    @Override
    public GraceJSONResult updateBlogUser(BlogUser bo) {
        try {
            beanFieldUtils.update(bo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return GraceJSONResult.ok(blogUserService.updateByBo(bo));
    }

    @Override
    public GraceJSONResult delBlogUser(String id) {
        return GraceJSONResult.ok(blogUserService.deleteById(id));
    }

    @Override
    public GraceJSONResult delBlogUserIds(List<String> ids) {
        return GraceJSONResult.ok(blogUserService.deleteWithValidByIds(ids, false));
    }

    @Override
    public GraceJSONResult getBlogUserById(String id) {
        return GraceJSONResult.ok(blogUserService.queryById(id));
    }

    @Override
    public LayuiTableResult getAllSysRoleList(String userId, PageQuery pageQuery) {
        return null;
    }

    @Override
    public GraceJSONResult updateStatus(String id, Integer status) {
        int i = blogUserService.updateStatus(id,status);
        if (i>0){
            return GraceJSONResult.ok();
        }else {
            return GraceJSONResult.error();
        }
    }

    @Override
    public GraceJSONResult resetPassword(String id) {
        BlogUser user = new BlogUser();
        user.setPassword(BCrypt.hashpw(userPassword));
        user.setId(id);
        Boolean aBoolean = blogUserService.updateByBo(user);
        if (aBoolean){
            return GraceJSONResult.ok("重置密码成功");
        }else {
            return GraceJSONResult.error("重置密码失败");
        }
    }

}
